package com.lagou.homework03;

import java.util.*;

public class FunctionList {
    private static Scanner sc = new Scanner(System.in);

    public static void add_student(List<Student> list)  {//增加学生信息，未对学号重复进行判断
        int id = id_insert();
        String name = name_insert();
        int age = age_insert();
        Student st = new Student(id, name, age);
        list.add(st);

    }

 /*   public static void delete_student(List<Student> list)  {//删除学生信息
        int i = id_insert();
        String s = name_insert();
        int i1 = age_insert();
        Student st = new Student(i, s, i1);
        list.remove(st);
    }*/


    public static void delete_student(final List<Student> list/*, final int id*/) {
        if (list == null) {
            return;
        }
        // 防止移除元素导致空指针，所以使用迭代器
        int id = id_insert();
        final Iterator<Student> iterator = list.iterator();
        while (iterator.hasNext()) {
            final Student student = iterator.next();
            if (student.getId() == id) {
                iterator.remove();
                break;
            }
        }
    }
    public static void modify_student(List<Student> list)  {//修改学生信息
        int id = id_insert();
        String name = name_insert();
        int age = age_insert();
        Iterator<Student> iterator = list.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            if (student.getId() == id)  {
                student.setId(id);
                student.setName(name);
                student.setAge(age);
                break;
            }
        }
    }

    public static void find_studet(List<Student> list)    {//查找
        if (list == null) {
            return;
        }
        int id = id_insert();
        Iterator<Student> iterator = list.iterator();
        while (iterator.hasNext()) {
            Student student = iterator.next();
            if (student.getId() == id) {
                System.out.println(student);
                break;
            }
        }

    }


    public static void ergodic_student(List<Student> list)   {//遍历
     for (int i = 0; i < list.size(); i++)   {
         Student s = list.get(i);
         System.out.println(s);
     }
    }


    public static int id_insert()  {
        System.out.println("请输入学号:");
        int id = sc.nextInt();
        return id;
    }
    public static String name_insert()  {
        System.out.println("请输入姓名:");
        String name = sc.next();
        return name;
    }
    public static int age_insert()  {
        System.out.println("请输入年龄:");
        int age = sc.nextInt();
        return age;
    }

}
